﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace ImageWater
{
    class ImageWater
    {

        InitFile Init = new InitFile();
        private string patch = Directory.GetCurrentDirectory();

        /// <summary>
        /// 图片加文字水印
        /// </summary>
        /// <param name="fileName"> </param>
        /// <param name="text">水印文字，如果是多行用分号隔开</param>
        /// <param name="img">图片</param>
        /// <param name="paddingTop">上边距</param>
        /// <param name="paddingLeft">左边距</param>
        /// <param name="textColor">文字颜色</param>
        /// <param name="textFont">字体</param>
        /// <param name="path">保存地址</param>
        /// <returns></returns>
        public bool AddWatermarkAndSave(string path, string fileName, string text, Image img,
                    int paddingTop, int paddingLeft, Color textColor, Font textFont)
        {
            try
            {
                text = text + ";" + "当前时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                textFont = new Font("宋体", 19);

                int imgheight = img.Height;
                int imgwidth = img.Width;
                if (img.Height > 1754 && img.Width < img.Height)
                {
                    imgheight = 1754;
                    imgwidth = (int)((double)img.Width / ((double)img.Height / 1754.00));
                }
                if (img.Width > img.Height && img.Width > 1754)
                {
                    imgwidth = 1754;
                    imgheight = (int)((double)img.Height / ((double)img.Width / 1754.00));
                }
                if (img.Width > img.Height && img.Width < 1754)
                {
                    imgwidth = 1754;
                    imgheight = (int)((double)img.Height / ((double)img.Width / 1754.00));
                }
                if (img.Width < img.Height && img.Height < 1754)
                {
                    imgheight = 1754;
                    imgwidth = (int)((double)img.Width / ((double)img.Height / 1754.00));
                }
                if (img.Width == img.Height)
                {
                    imgheight = 1240;
                    imgwidth = 1240;
                }
                Bitmap Cover = new Bitmap(img, imgwidth, imgheight);
                //加载水印文件
                // string sitamp = @"sitamp.png";
                //string shuiyin = patch +"\\"+ sitamp;
                //System.Drawing.Image water = System.Drawing.Image.FromFile(shuiyin);
                //Bitmap waters = new Bitmap(water, 300, 300);
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(Cover);
                System.Drawing.Brush b = new SolidBrush(textColor);
                //g.DrawImage(waters, new Rectangle(Cover.Width - waters.Width - 80, 120, waters.Width, waters.Height), 0, 0, waters.Width, waters.Height, GraphicsUnit.Pixel);
                //g.DrawImage(waters, new Rectangle(Cover.Width - waters.Width - 580, 120, waters.Width, waters.Height), 0, 0, waters.Width, waters.Height, GraphicsUnit.Pixel);
                g.Dispose();
                Cover.Save(fileName, ImageFormat.Jpeg);
                Cover.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                Init.WriteLog(ex.ToString(), true);
                return false;
            }
        }
    }
}
